# Template file for 'openoffice'
pkgname=openoffice
version=4.1.15
revision=1
_vmaj=4
_lang=en-US
archs="x86_64 i686"
create_wrksrc=yes
depends="desktop-file-utils hicolor-icon-theme freetype glibc gtk+ shared-mime-info"
short_desc="Free and Open Productivity Suite"
maintainer="MarcoAPC <marcoaureliopc@gmail.com>"
license="Apache-2.0"
homepage="https://www.openoffice.org"
nostrip=yes

# To install different language version adjust the following lines: _lang and checksum. 
# available: ast, bg, ca, ca-XR, ca-XV, cs, da, de, el, en-GB, en-US, es, eu, fi, fr, gd, gl, he, hi, hu, it, ja, km, ko, lt, nb, nl, pl, pt, pt-BR, ru, SDK, sk, sl, sr, sv, ta, th, tr, vi, zh-CN, zh-TW

case "$XBPS_TARGET_MACHINE" in
x86_64)
	distfiles="https://downloads.apache.org/openoffice/${version}/binaries/${_lang}/Apache_OpenOffice_${version}_Linux_x86-64_install-rpm_${_lang}.tar.gz"
	checksum=6cbfc129c0e908ed0cb6acfc6a112879f79a1b5e7f3fe476e835e71b81f61073
	;;
i686)
	distfiles="https://downloads.apache.org/openoffice/${version}/binaries/${_lang}/Apache_OpenOffice_${version}_Linux_x86_install-rpm_${_lang}.tar.gz"
	checksum=338163ab9105ecfb2bc847e43902bcdc4a9a2826ecbbda2f5e85bb5b909949d2
	;;
*)
	broken="No distfiles available for this target"
	;;
esac

do_install() {
	_source_dirs=${_lang}/RPMS

	local dir file
	for dir in "${_source_dirs[@]}"; do
		dir=${wrksrc}/${dir}
		( cd "${dir}" ) # check existing
		for file in $( find "${dir}" -type f -name '*.rpm' ); do
		echo "Extracting ${file##*/}"
		bsdtar -xf "${file}"
			if ! [[ $file == */desktop-integration/* && $file != *-freedesktop-menus-* ]] &&
				! [[ $file == */jre-* ]]; then
				echo "${file}"
			elif type -p msg >/dev/null; then
				echo "Skipping ${file##*/}" >&2
			else
				echo "Skipping ${file##*/}" >&2
			fi
		done
	done

	# remove symlink to avoid conflict with libreoffice-common
    # (not used in the desktop files)
	[[ -h usr/bin/soffice ]] && rm -f usr/bin/soffice
	sed -i -re "1s;^#! *(/usr(/local)?)?/bin/(env +)?python(2[^ ]*)?( |$);#!/usr/bin/env python2 ;" $(
		find opt/openoffice${_vmaj}/program/python-core-2.7.18/lib -type f -name '*.py'
	)

	# Remove unused
	rm -rf usr/share/application-registry
	rm -rf usr/share/applnk-redhat
	rm -rf usr/lib/menu

	vcopy opt /
	vcopy usr /
}
